﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Token.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Represents a LightHouse token
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Xml.Linq;

namespace LighthouseNET
{
    using System;

    /// <summary>
    /// Represents a LightHouse token
    /// </summary>
    public class Token
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Token"/> class. 
        /// </summary>
        /// <param name="createdAt">
        /// The created at.
        /// </param>
        /// <param name="note">
        /// The note.
        /// </param>
        /// <param name="projectId">
        /// The project id.
        /// </param>
        /// <param name="isReadOnly">
        /// if set to <c>true</c> [is read only].
        /// </param>
        /// <param name="tokenString">
        /// The token string.
        /// </param>
        /// <param name="userId">
        /// The user id.
        /// </param>
        /// <param name="account">
        /// The account.
        /// </param>
        private Token(
            DateTime createdAt,
            string note,
            int projectId,
            bool isReadOnly,
            string tokenString,
            int userId,
            string account)
        {
            CreatedAt = createdAt;
            Node = note;
            ProjectId = projectId;
            ReadOnly = isReadOnly;
            TokenString = tokenString;
            UserId = userId;
            Account = account;
        }

        #region Properties
        /// <summary>
        /// Gets the created at.
        /// </summary>
        public DateTime CreatedAt { get; private set; }

        /// <summary>
        /// Gets the node.
        /// </summary>
        public string Node { get; private set; }

        /// <summary>
        /// Gets the project id.
        /// </summary>
        public int ProjectId { get; private set; }

        /// <summary>
        /// Gets a value indicating whether [read only].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [read only]; otherwise, <c>false</c>.
        /// </value>
        public bool ReadOnly { get; private set; }

        /// <summary>
        /// Gets the token string.
        /// </summary>
        public string TokenString { get; private set; }

        /// <summary>
        /// Gets the user id.
        /// </summary>
        public int UserId { get; private set; }

        /// <summary>
        /// Gets the account.
        /// </summary>
        public string Account { get; private set; } 
        #endregion

        /// <summary>
        /// Parses the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Parsed token</returns>
        /// <exception cref="ArgumentNullException">Element is null or empty.</exception>
        internal static Token Parse(XElement element)
        {
            if (element == null || element.IsEmpty)
            {
                throw new ArgumentNullException("element");
            }

// ReSharper disable PossibleNullReferenceException
            var createdAt = Globals.DateTimeFromLighthouse(element.Element("created-at").Value);
            var note = element.Element("note").Value;
            int projectId;
            int.TryParse(element.Element("project-id").Value, out projectId);
            var readOnly = bool.Parse(element.Element("read-only").Value);
            var userId = int.Parse(element.Element("user-id").Value);
            var account = element.Element("account") != null ? element.Element("account").Value : string.Empty;
            var tokenString = element.Element("token").Value;
// ReSharper restore PossibleNullReferenceException

            return new Token(
                createdAt,
                note,
                projectId,
                readOnly,
                tokenString,
                userId,
                account);
        }
    }
}
